<?php

namespace app\models;

use Yii;
use yii\db\Expression;
/**
 * This is the model class for table "sms_log".
 *
 * @property integer $id
 * @property string $phone
 * @property string $dowhat
 * @property string $code
 * @property string $result
 * @property string $error_code
 * @property string $error_msg
 * @property integer $used
 * @property string $use_time
 * @property string $create_time
 */
class SmsLog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sms_log';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['phone', 'dowhat', 'code', 'result', 'error_code', 'error_msg', 'used'], 'required'],
            [['used'], 'integer'],
            [['use_time', 'create_time'], 'safe'],
            [['phone', 'dowhat'], 'string', 'max' => 20],
            [['code'], 'string', 'max' => 6],
            [['result'], 'string', 'max' => 80],
            [['error_code', 'error_msg'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'phone' => 'Phone',
            'dowhat' => 'Dowhat',
            'code' => 'Code',
            'result' => 'Result',
            'error_code' => 'Error Code',
            'error_msg' => 'Error Msg',
            'used' => 'Used',
            'use_time' => 'Use Time',
            'create_time' => 'Create Time',
        ];
    }

        /**
         * 生成并通过服务商发送验证码
         */
        public function requestSms($phone,$dowhat)
        {
          //生成6位随机数
          $code=self::generateVerifyCode();
          $smslog=new SmsLog();
          $smslog->phone= $phone;
          $smslog->dowhat = $dowhat;
          $smslog->code=$code;
          $smslog->used=0;
          $smslog->use_time=new Expression('NOW()');
          $smslog->create_time=new Expression('NOW()');

          /*$ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");

          curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          curl_setopt($ch, CURLOPT_HEADER, FALSE);

          curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
          curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-b761c24f77fc5d77769d5a442ccacc10');

          curl_setopt($ch, CURLOPT_POST, TRUE);
          curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => '13761428267','message' => '验证码：'.$code.'【NEA用户注册】'));

          $res = curl_exec( $ch );
          curl_close( $ch );
          //$res  = curl_error( $ch );
          var_dump($res);
          //{"error":0,"msg":"ok"}*/
          $sysconfig = SysConfig::getSysConfig();
          $statusStr = array(
          "0" => "短信发送成功",
          "-1" => "参数不全",
          "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
          "30" => "密码错误",
          "40" => "账号不存在",
          "41" => "余额不足",
          "42" => "帐户已过期",
          "43" => "IP地址限制",
          "50" => "内容含有敏感词"
          );
          $smsapi = "http://api.smsbao.com/";
          $user = $sysconfig->sms_user; //短信平台帐号
          $pass = md5($sysconfig->sms_password); //短信平台密码
          $content='验证码：'.$code.'【贝恩资本会员注册】';//要发送的短信内容
          $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
          $result =file_get_contents($sendurl) ;
          //$result="0";
          $smslog->result=$result;
          $smslog->error_code=$result;
          $smslog->error_msg=$statusStr[$result];
          $returnMsg=$statusStr[$result];
          if(!$smslog->save()){
            if($result==="0"){
              $returnMsg="短信入库失败。";
            }
          }

          return $returnMsg;

        }

        /**
         * 生成短信验证码
         */
        public function generateVerifyCode(){
          srand((double)microtime()*1000000);//create a random number feed.
          //$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";  // 36;
          $ychar="0,1,2,3,4,5,6,7,8,9"; //10
          $list=explode(",",$ychar);
          $authnum='';
          for($i=0;$i<6;$i++){
            $randnum=rand(0,9); //0,35
            $authnum.=$list[$randnum];
          }
          return $authnum;
        }

        /**
         * 获取短信余额
         */
        public function querySmsDeposit(){
          /*$ch = curl_init();

          curl_setopt($ch, CURLOPT_URL , "http://sms-api.luosimao.com/v1/status.json");
          curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
          curl_setopt($ch, CURLOPT_HEADER, FALSE);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

          curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
          curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-b761c24f77fc5d77769d5a442ccacc10');

          $res =  curl_exec( $ch );
          curl_close( $ch );
          //$res  = curl_error( $ch );
          var_dump($res);
          //{"error":0,"deposit":"1119987446"}
          if($res->error===0){
            return $res->deposit;
          }else{
            return '获取短信余额失败。';
          }*/
        }


}
